from django.shortcuts import render,redirect
from user.form import CreateUserForm,LoginForm
# Create your views here.
from movie.models import MovieInfo
from comment.form import CommentForm
from comment.models import Comment
from django.utils.timezone import now
from movie.models import Movie_recent_visit
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
def index(request):
    usercreateform=CreateUserForm()
    loginform=LoginForm()
    movie_req=MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='requestedmovie',movie_degree__degree='1')
    movie_rat=MovieInfo.objects.filter(movie_rtpcatgory__rpt_category='recent',movie_degree__degree='1')
    movie_latest=MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='latestmovie',movie_degree__degree='1')
    movie_top=MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='topmovie',movie_degree__degree='1')
    movie_pop=MovieInfo.objects.filter(movie_rtpcatgory__rpt_category='popularity',movie_degree__degree='1')
    movie_toprating=MovieInfo.objects.filter(movie_rtpcatgory__rpt_category='toprating',movie_degree__degree='1')
    movie_large=MovieInfo.objects.filter(movie_degree__degree='2')[0:4]
    movie_second_large=MovieInfo.objects.filter(movie_degree__degree='3')

    data={'usercreateform':usercreateform,
          'loginform':loginform,
          'movie_req':movie_req,
          'movie_rat':movie_rat,
          'movie_latest':movie_latest,
          'movie_top':movie_top,
          'movie_pop':movie_pop,
          'movie_toprating':movie_toprating,
          'movie_large':movie_large,
          'movie_second_large':movie_second_large

    }

    return render(request,'movie/index.html',data)


def single(request,pk):
    visit_time=now()
    movie_id=MovieInfo.objects.filter(pk=pk)[0]
    Movie_recent_visit.objects.create(movie_id=movie_id,visit_time=visit_time)
    movie_detail=MovieInfo.objects.filter(pk=pk)[0]
    movie_recent_vist=Movie_recent_visit.objects.all().order_by('-pk')
    movie_recent_vist=movie_recent_vist[1:5]
    usercreateform=CreateUserForm()
    loginform=LoginForm()
    commentform=CommentForm()
    comment=Comment.objects.filter(comment_movie=pk).order_by('-comment_date')
    movie_hottopic=MovieInfo.objects.filter(movie_hottopic=True)
    data={
          'movie_detail': movie_detail,
          'usercreateform':usercreateform,
          'loginform':loginform,
          'commentform':commentform,
          'comment':comment,
          'movie_hottopic':movie_hottopic,
          'movie_recent_vist':movie_recent_vist,
    }
    return  render(request,'movie/single.html',data)


def page(request):
    usercreateform=CreateUserForm()
    loginform=LoginForm()
    movie_comedy=MovieInfo.objects.filter(movie_category__category='comedy',movie_degree__degree='1')
    movie_honor=MovieInfo.objects.filter(movie_category__category='honor',movie_degree__degree='1')
    movie_top=MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='topmovie',movie_degree__degree='1')
    movie_requested=MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='requestedmovie',movie_degree__degree='1')
    page_contain=10
    paginator=Paginator(movie_requested,page_contain)
    page=request.GET.get('page')
    try:
        movie_requested=paginator.page(page)
    except PageNotAnInteger:
        movie_requested=paginator.page(1)
    except EmptyPage:
        movie_requested=paginator.page(paginator.num_pages)

    data={
        'movie_comedy':movie_comedy,
        'movie_honor':movie_honor,
        'movie_top':movie_top,
        'movie_requested':movie_requested,
        'usercreateform':usercreateform,
        'loginform':loginform,

    }

    return render(request,'movie/genre3.html',data)


def honor_page(request):
    limit=10
    honor_movie=MovieInfo.objects.filter(movie_category__category='honor',movie_degree__degree='1')
    paginator=Paginator(honor_movie,limit)
    page=request.GET.get('page')
    try:
        honor_movie=paginator.page(page)
    except PageNotAnInteger:
        honor_movie=paginator.page(1)
    except EmptyPage:
        honor_movie=paginator.page(paginator.num_pages)
    movie_top = MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='topmovie', movie_degree__degree='1')
    usercreateform = CreateUserForm()
    loginform = LoginForm()

    data={
        'honor_movie':honor_movie,
        'movie_top':movie_top,
        'usercreateform':usercreateform,
        'loginform':loginform

    }

    return render(request,'movie/horror.html',data)


def comedy_page(request):
    usercreateform = CreateUserForm()
    loginform = LoginForm()
    limit = 10
    movie_comedy=MovieInfo.objects.filter(movie_category__category='comedy',movie_degree__degree='1')
    paginator=Paginator(movie_comedy,limit)
    page=request.GET.get('page')
    try:
        movie_comedy=paginator.page(page)
    except PageNotAnInteger:
        movie_comedy=paginator.page(1)
    except EmptyPage:
        movie_comedy=paginator.page(paginator.num_pages)
    movie_top = MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='topmovie', movie_degree__degree='1')

    data={
        'movie_comedy':movie_comedy,
        'movie_top':movie_top,
        'usercreateform':usercreateform,
        'loginform':loginform
    }
    return render(request,'movie/comedy.html',data)

def series(request):

    movie_first_banner=MovieInfo.objects.filter(movie_degree__degree='1')
    movie_second_large=MovieInfo.objects.filter(movie_degree__degree='2')
    movie_requested=MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='requestedmovie',movie_degree__degree='1')
    movie_top = MovieInfo.objects.filter(movie_lrtcatgory__LRT_category='topmovie', movie_degree__degree='1')


    data={
        'movie_first_banner':movie_first_banner,
        'movie_second_large':movie_second_large,
        'movie_requested':movie_requested,
        'movie_top':movie_top
    }

    return render(request,'movie/series.html',data)

